package com.rf.richfitwheel.admin.sys.service.impl;

import com.rf.richfitwheel.admin.sys.dao.RoleMapper;
import com.rf.richfitwheel.admin.sys.dao.UserRoleMapper;
import com.rf.richfitwheel.admin.sys.model.Role;
import com.rf.richfitwheel.admin.sys.model.RoleExample;
import com.rf.richfitwheel.admin.sys.model.UserRole;
import com.rf.richfitwheel.admin.sys.model.UserRoleExample;
import com.rf.richfitwheel.admin.sys.service.SysUserRoleService;
import com.rf.richfitwheel.common.contants.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 用户与角色对应关系
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        //先删除用户与角色关系
//		this.deleteByMap(new MapUtils().put("user_id", userId));

        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }

        //保存用户与角色关系
        List<UserRole> list = new ArrayList<UserRole>(roleIdList.size());
        for (Long roleId : roleIdList) {
            UserRole record = new UserRole();
//			record.setUserId(userId);
//			record.setRoleId(roleId);

            list.add(record);
            userRoleMapper.insertSelective(record);
        }
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
//		return record.queryRoleIdList(userId);
        return null;
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
//		return baseMapper.deleteBatch(roleIds);
        return 0;
    }

    @Override
    public List<UserRole> getListByUserId(String userId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        return userRoles;
    }

    @Override
    public List<UserRole> getListByUserIdAndOrgId(String userId, String orgId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        return userRoles;
    }
    
    @Override
    public List<UserRole> getListByUserIdAndOrgId(String userId, String orgId, String moduleType) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria roleCriteria = roleExample.createCriteria();
        List<String> moduleTypeList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
        moduleTypeList.add(Constant.SystemModule.sys.name());
        roleCriteria.andModuleTypeIn(moduleTypeList);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        List<UserRole> userRoles = new ArrayList<>();
        if(!roleList.isEmpty()){
            criteria.andRoleIdIn(roleList.stream().map(Role::getId).collect(Collectors.toList()));
            userRoles = userRoleMapper.selectByExample(example);
        }
        return userRoles;
    }

    @Override
    public void deleteByUserIdAndOrgId(String userId, String orgId) {
        List<UserRole> userRoles = this.getListByUserIdAndOrgId(userId, orgId);
        for (UserRole userRole : userRoles) {
            userRoleMapper.deleteByPrimaryKey(userRole.getId());
        }
    }

    @Override
    public void deleteByUserId(String userId) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        for (UserRole userRole : userRoles) {
            userRoleMapper.deleteByPrimaryKey(userRole.getId());
        }
    }
    
    /**
     * 只删除对应模块的记录
     *
     * @param userId     用户名
     * @param moduleType 模块
     */
    @Override
    public void deleteByUserIdAndModuleType(String userId, String moduleType){
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        List<String> modList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
        modList.add(Constant.SystemModule.sys.name());
        params.put("moduleType", modList);
        userRoleMapper.deleteByUserIdAndModuleType(params);
    }
}
